<?php

namespace IDock\Insurance\LIHI\Request;

use GuzzleHttp\Psr7\Stream;
use IDock\Http\RequestFormat;
use IDock\IDock\IDockEnums;
use IDock\Insurance\LIHI\LIHIEnums;
use IDock\Insurance\LIHI\Request\AllLIHIRequest;
use IDock\Insurance\LIHI\Response\UploadFileResponse;
use IDock\Visitor\Premium\PremiumInquiryVisitor;
use IDock\Visitor\UploadFileVisitor;

class UploadFileRequest extends AllLIHIRequest
{
    public $route = 'zuul/cp/upload/uploadDocument';

    protected $response = UploadFileResponse::class;

    protected $urlTip = '影像上传';

    protected $inLog = true;

    protected $format = RequestFormat::MUL;

    /**
     * @var  UploadFileVisitor $visitor
     */
    protected $visitor;

    public function generate(): array
    {
        $this->flowId = $this->visitor->argc['flowId'] ?? '';
        $file = $this->visitor->fileList[0] ?? [];
        $fileType = LIHIEnums::$uploadFileType[$file['type']] ?? '';
        $fileTypeName = LIHIEnums::$uploadFileTypeName[$file['type']] ?? '';
        if(!$fileType){
            throw new \Exception("不支持该文件类型");
        }
        if(!$fileTypeName){
            throw new \Exception("文件名称不能为空");
        }
        $arr = [
            [
                'name' => 'businessNo',
                'contents' => $this->visitor->biInquiryNo ?: $this->visitor->ciInquiryNo
            ],
            [
                'name' => 'sort',
                'contents' => '1'
            ],
            [
                'name' => 'code',
                'contents' => $fileType
            ],
            [
                'name' => 'fileTitle',
                'contents' => $fileTypeName
            ],
            [
                'name' => 'fromSystem',
                'contents' => 'SOA'
            ],
        ];
        $ocrType = LIHIEnums::$uploadOCRFileType[$file['type']] ?? '';
        if(count($this->visitor->fileList) == 1){
            if($ocrType == 'ocrFront'){
                //正面照
                array_push($arr,[
                    'name' => 'ocrFlag',
                    'contents' => '1',
                ],[
                    'name' => 'ocrFront',
                    'contents' => fopen($file['url'],'r'),
                    'filename' => sha1(mt_rand(100000,999999)).basename($file['url']),
                ]);
            }elseif ($ocrType == 'ocrBack'){
                array_push($arr,[
                    'name' => 'ocrFlag',
                    'contents' => '1',
                ],[
                    'name' => 'ocrBack',
                    'contents' => fopen($file['url'],'r'),
                    'filename' => sha1(mt_rand(100000,999999)).basename($file['url']),
                ]);
            }else{
                $arr[] = [
                    'name' => 'files',
                    'contents' => fopen($file['url'],'r'),
                    'filename' => sha1(mt_rand(100000,999999)).basename($file['url'])
                ];
            }
        }else{
            $file1 = $this->visitor->fileList[1] ?? [];
            $ocrType1 = LIHIEnums::$uploadOCRFileType[$file1['type']] ?? '';
            array_push($arr,[
                'name' => 'ocrFlag',
                'contents' => '1',
            ],[
                'name' => $ocrType,
                'contents' => fopen($file['url'],'r'),
                'filename' => sha1(mt_rand(100000,999999)).basename($file['url']),
            ],[
                'name' => $ocrType1,
                'contents' => fopen($file1['url'],'r'),
                'filename' => sha1(mt_rand(100000,999999)).basename($file1['url']),
            ]);
        }
        return $arr;
    }

}
